package com.eight.login.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import com.eight.common.pojo.TbMember;
import com.eight.common.vo.Result;
import com.eight.login.service.StudentService;
import com.eight.login.vo.LoginVo;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @program: qy8-education1
 * @description:
 * @author: 张亚祥
 * @create: 2023-05-31 20:09
 **/
@RestController
@RequestMapping("student/admin")
@Api("登录功能")
public class StudentController {
    @Resource
    private StudentService studentService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @PostMapping("qianLogin")
    public Result qianLogin(@RequestBody LoginVo loginVo){
        TbMember tbMember = studentService.FindByNameAndpwd(loginVo.getMobile(), loginVo.getPassword());
        if(tbMember != null){
            if(tbMember.getIsDeleted().equals(0)){
                String token = IdUtil.simpleUUID();
                tbMember.setPassword(null);
                tbMember.setToken(token);
                redisTemplate.opsForValue().set("user::"+token, JSON.toJSONString(tbMember),30, TimeUnit.MINUTES);

                return  Result.success(tbMember);
            }else {
                return Result.fail("该账号被冻结了,请联系管理员");
            }
        }
        return Result.fail("账号或密码错误");
    }
    }


